package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.ArticleMapper;
import com.j283_5.wisdom_park.pojo.Article;
import com.j283_5.wisdom_park.pojo.Tenement;
import com.j283_5.wisdom_park.service.ArticleService;
import com.j283_5.wisdom_park.vo.contract_management.CustomerVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class ArticleServiceImpl  implements ArticleService {
    @Resource
    private ArticleMapper mapper;
    /**
     * 多条件查询文章信息
     * @param article
     * @return
     */
    @Override
    public Map<String,Object> findArticle(Article article, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<Article> list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findArticleCount(article);
        //如果总条数大于0  才去查询数据
        if(countRows!=null && countRows>0){
            list=mapper.findArticle(article,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listArticle",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    /**
     * 文章新增
     * @param article
     * @return
     */
    @Override
    public ResultData insertArticle(Article article) {
        Article article1=new Article();
        article1.setArtTitle(article.getArtTitle());
        article1.setArtDitch(article.getArtDitch());
        article1.setArtChannel(article.getArtChannel());
        article1.setArtType(article.getArtType());
        article1.setArtKeyWord(article.getArtKeyWord());
        article1.setArt_introduction(article.getArt_introduction());
        article1.setAdminId(article.getAdminId());
        article1.setArt_temp(article.getArt_temp());

        int insert = mapper.insert(article1);
        if (insert>0){
            return ResultData.ok().message("新增成功");
        }else {
            return ResultData.error().message("新增失败");
        }
    }


    /**
     * 查询添加的下拉框
     * @param
     * @return
     */
    @Override
    public Map<String,Object>  findInsert() {

        QueryWrapper<Article> articleQueryWrapper1 = new QueryWrapper<>();
        articleQueryWrapper1.select("distinct art_ditch ");
        List<Article> art_ditch = mapper.selectList(articleQueryWrapper1);

        QueryWrapper<Article> articleQueryWrapper2 = new QueryWrapper<>();
        articleQueryWrapper2.select("distinct art_channel ");
        List<Article> art_channel = mapper.selectList(articleQueryWrapper2);

        QueryWrapper<Article> articleQueryWrapper3 = new QueryWrapper<>();
        articleQueryWrapper3.select("distinct art_type ");
        List<Article> art_type = mapper.selectList(articleQueryWrapper3);

        return new HashMapUtils()
                .addData("art_ditch",art_ditch)
                .addData("art_channel",art_channel)
                .addData("art_type",art_type)
                .getMap();
    }

    /**
     * 文章删除
     * @param articleId
     * @return
     */
    @Override
    public ResultData deleteArticle(List<Integer> articleId) {
        UpdateWrapper<Article> ArticleWrapper = new UpdateWrapper<>();
        ArticleWrapper.set("art_delete",1);
        ArticleWrapper.in("art_id",articleId);
        int i = mapper.update(null, ArticleWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }
}
